import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/',
    name: 'layput',
    redirect: '/welcome',
    component: () => import('@/views/layout/index.vue'),
    children: [
      {
        path: '/categories',
        name: 'categories',
        component: () => import('@/views/layout/categories/categories.vue')
      },
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('@/components/welcome.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('@/views/layout/goods/goods.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('@/views/layout/orders/orders.vue')
      },
      {
        path: '/add',
        name: 'add',
        component: () => import('@/views/layout/goods/add.vue')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('@/views/layout/params/params.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('@/views/layout/reports/reports.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('@/views/layout/rights/rights.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('@/views/layout/roles/roles.vue')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('@/views/layout/user/user.vue')
      },
      {
        path: '/login',
        name: 'login',
        component: () => import('@/views/layout')
      }
    ]
  },
  {
    path: '*',
    name: 'notfound',
    component: () => import('@/components/notfound.vue')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})
// router.afterEach((to,from)=>{
//   if(from.path==='/login'){

//   }
// })
export default router
